import hashlib

# 用于日志的记录
import pickle

from afast_core.core_libs.redis_cache import DEFAULT_PREFIX, DEFAULT_NAMESPACE


def pickle_encoder(data):
    '''
    编码方式
    :param data:
    :return:
    '''
    return pickle.dumps(data)


def pickle_decoder(data):
    '''
    解码方式
    :param data:
    :return:
    '''
    return pickle.loads(data)


def key_builder(
        module: str,
        name: str,
        args,
        kwargs,
        prefix: str = DEFAULT_PREFIX,
        namespace: str = DEFAULT_NAMESPACE,
):
    """构建缓存键"""
    prefix = f"{prefix}:{namespace}:"
    # 前缀+命名空间
    cache_key = (prefix + hashlib.md5(f"{module}:{name}:{args}:{kwargs}".encode()).hexdigest())
    return cache_key